/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.web.jsf.beans.common;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;

import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;
import org.richfaces.model.Arrangeable;
import org.richfaces.model.ArrangeableState;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.GenericDao;

/**
 * @author Jorge E. Villaverde
 * 
 */
public abstract class DaoDataModel<T, PK extends Serializable> extends
		ExtendedDataModel<T> implements Arrangeable {

	private GenericDao<T, PK> dao;
	private PK rowKey;
	private ArrangeableState state;

	public DaoDataModel(GenericDao<T, PK> dao) {
		super();
		this.dao = dao;
	}

	@Override
	public void arrange(FacesContext context, ArrangeableState state) {
		this.state = state;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setRowKey(Object key) {
		this.rowKey = (PK) key;
	}

	@Override
	public Object getRowKey() {
		return rowKey;
	}

	@Override
	public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument) {
        SequenceRange sr = (SequenceRange) range;
        int startPosition = sr.getFirstRow();
        int maxResult = sr.getRows();

		List<T> data;
		try {
			data = dao.findEntries(state, startPosition, maxResult);
			for (T t : data) {
				visitor.process(context, getId(t), argument);
			}
		} catch (DaoException e) {
		}
	}

	@Override
	public boolean isRowAvailable() {
		return rowKey != null;
	}

	@Override
	public int getRowCount() {
		int rowCount = -1;
		try {
			rowCount = (int) dao.countByArrangeableState(state);
		} catch (DaoException e) {
		}
		return rowCount;
	}

	@Override
	public T getRowData() {
		try {
			return dao.findById(rowKey);
		} catch (DaoException e) {
		}
		return null;
	}

	@Override
	public int getRowIndex() {
		return -1;
	}

	@Override
	public void setRowIndex(int rowIndex) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Object getWrappedData() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setWrappedData(Object data) {
		throw new UnsupportedOperationException();
	}

	protected ArrangeableState getArrangeableState() {
		return state;
	}

	protected abstract Object getId(T t);
}
